package cn.scu.ne04jdemo.controller;

import cn.scu.ne04jdemo.entitiy.ScheDevInfoEntity;
import cn.scu.ne04jdemo.service.ScheDevInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/sche/dev/info")
public class ScheDevInfoController {

    @Autowired
    private ScheDevInfoService scheDevInfoService;

    @GetMapping("/findPage")
    public ResponseEntity<IPage> findPage(@RequestParam(required = false) String devName,
                                            @RequestParam(required = false) String devTypeId,
                                            @RequestParam(required = false) String devState,
                                            @RequestParam(defaultValue = "1") int pageNum,
                                            @RequestParam(defaultValue = "10") int pageSize) {
        LambdaQueryWrapper<ScheDevInfoEntity> queryWrapper = new QueryWrapper<ScheDevInfoEntity>().lambda()
                .like(StringUtils.isNotBlank(devName), ScheDevInfoEntity::getDevName, devName)
                .eq(StringUtils.isNotBlank(devTypeId), ScheDevInfoEntity::getDevTypeId, devTypeId)
                .eq(StringUtils.isNotBlank(devState), ScheDevInfoEntity::getDevState, devState)
                .orderByDesc(ScheDevInfoEntity::getCreateDate);
        IPage<ScheDevInfoEntity> page = scheDevInfoService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return ResponseEntity.ok(page);
    }


    @PostMapping("/delById")
    public Boolean delById(@RequestBody ScheDevInfoEntity scheDevInfoEntity) {
        try {
            if(ObjectUtils.isNotNull(scheDevInfoEntity)){
                String id = scheDevInfoEntity.getId();
                scheDevInfoService.removeById(id);
            }else{
                throw new Exception();
            }

        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @PostMapping("/insert")
    @ResponseBody
    public Boolean insert(@RequestBody ScheDevInfoEntity scheDevInfoEntity){
//        try {
//            if(!ObjectUtils.isNotNull(scheDevInfoEntity))
//                scheDevInfoEntity = new ScheDevInfoEntity();
//            scheDevInfoService.save(scheDevInfoEntity);
//        }catch (Exception e){
//            e.printStackTrace();
//            return false;
//        }
//        return true;
        try {
            return this.scheDevInfoService.insert(scheDevInfoEntity);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @PostMapping("/update")
    @ResponseBody
    public Boolean update(@RequestBody ScheDevInfoEntity scheDevInfoEntity){
        try {
            scheDevInfoService.updateById(scheDevInfoEntity);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

}
